local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();
local CallBackManager = ClientOHOH.load.CallBackManager();
local FunctionList = ClientOHOH.load.FunctionList();
local LanguageHelper= _G.LanguageHelper
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local GroupReward_cfg = ClientOHOH.config.GroupReward
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local InstanceManager = ClientOHOH.load.InstanceManager();
local ItemTool=_G.CommonOHOH.load.ItemTool()
local GeneralTool=_G.CommonOHOH.load.GeneralTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	local JionGroup=workspace.JionGroup
	self.TipLable=ClientOHOH:GetPath_Loop("Attachment.BillboardGui.Frame.TextLabel",JionGroup.PrimaryPart)

	self.TipLable.Text=LanguageHelper:GetText(40001)
	self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());
	CallBackManager:AddRbxConnect(self,JionGroup.PrimaryPart.Touched, function(obj,chPart)
		if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart) then
			return 
		end
		if self.TouchEd then
			return
		end
		local p= chPart:FindFirstAncestorOfClass("Model")
		if p then
			local character = InstanceManager:FindObject(p);
			if character and character.player and character.player==self.parent then
				
				if self.parent.main_inst:IsInGroup(GroupReward_cfg.groupId) then
					if self:isCanClaim() then
						self.TouchEd=true
						self:ChangePlay(self.open)
						self.Opening=true
						delay(3,function()
							self.Opening=nil
						end)
						-- print("reward--A->")

						local reward=ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.group_reward)
						
						-- print("reward--B->",reward)
						if reward then
							-- character:GetComponent(ClientOHOH.load.Client_CharacterAttack()):Attack()
							ClientOHOH.load.Client_AudioManager():PlayGloble("claim")
							-- local e=ClientOHOH:GetPath_Loop("BoxDie",game.ReplicatedStorage.Resource.Effect):Clone()
							-- require(e.PlayCtrl).Play(workspace.Sign.Touch.Position)
							for k,v in pairs(reward) do
								if k=="egg" then
									continue
								end
								ItemTool.FlyingItem_1(k,v)
							end
						else
							GlobalTipGui:Show(LanguageHelper:GetText(30006))
						end
						self.TouchEd=nil
					else
						return
					end

				else
					GlobalTipGui:Show(LanguageHelper:GetText(300001))
				end
			end
		end
	end);
	self:AddUpdate(self.Update,1);

	self.idle = JionGroup.Humanoid:LoadAnimation(JionGroup.animations.Treasureidle)
	self.open = JionGroup.Humanoid:LoadAnimation(JionGroup.animations.TreasureOpen)
	self.openidle = JionGroup.Humanoid:LoadAnimation(JionGroup.animations.TreasureOpenidle)
	self.open.Looped = false
	self.openidle.Looped = true
	self.idle.Looped = true
	self.Current=nil

end;
function M.Update(self)
	if self.parent.main_inst:IsInGroup(GroupReward_cfg.groupId) then

		local rid=self.DataBase:GetData("groupReward").id
		local cfg=GroupReward_cfg.reward[rid]
		assert(cfg,"cfg==nil")
		local t=self.DataBase:GetData("groupReward").time-os.time()
		-- print("-------------------->",self.DataBase:GetData("groupReward").time,t)
		if t>0 then-- 不可以领取
			self:ChangePlay(self.idle)
			self.TipLable.Text=GeneralTool.secondsToHHMM(t)
		else --可领取
			self:ChangePlay(self.openidle)
			self.TipLable.Text=LanguageHelper:GetText(8)
		end
	else --没加入群组
		self:ChangePlay(self.idle)
		self.TipLable.Text=LanguageHelper:GetText(40001)
	end
end
function M.ChangePlay(self,animation)
	if self.Opening then
		return
	end
	if self.Current~=animation then
		if self.Current then
			self.Current:Stop()
		end
		animation:Play()
		self.Current=animation
	end
end

function M.isCanClaim(self)
	local rid=self.DataBase:GetData("groupReward").id
	local cfg=GroupReward_cfg.reward[rid]
	assert(cfg,"cfg==nil")
	if os.time()>self.DataBase:GetData("groupReward").time then
		return true
	end
	return false
end

function M.dtor(self)
	M.super.dtor(self);
end;

return M
